package com.swak.boot.config.jdbc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.ibatis.session.ExecutorType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.swak.Constants;
import com.swak.utils.Sets;

/**
 * Configuration properties for MyBatis.
 *
 * @author Eddú Meléndez
 * @author Kazuki Shimizu
 */
@ConfigurationProperties(prefix = Constants.MYBATIS_PREFIX)
public class MybatisProperties {

	private String[] mapperLocations;
	private String typeAliasesPackage;
	private String typeHandlersPackage;
	private ExecutorType executorType;
	private Properties configurationProperties;
	private boolean openMasterSlave = false;
	private boolean openSharding = false;
	private boolean lazyLoadingEnabled = true;
	private boolean aggressiveLazyLoading = false;

	public boolean isLazyLoadingEnabled() {
		return lazyLoadingEnabled;
	}

	public void setLazyLoadingEnabled(boolean lazyLoadingEnabled) {
		this.lazyLoadingEnabled = lazyLoadingEnabled;
	}

	public boolean isAggressiveLazyLoading() {
		return aggressiveLazyLoading;
	}

	public void setAggressiveLazyLoading(boolean aggressiveLazyLoading) {
		this.aggressiveLazyLoading = aggressiveLazyLoading;
	}

	public boolean isOpenMasterSlave() {
		return openMasterSlave;
	}

	public void setOpenMasterSlave(boolean openMasterSlave) {
		this.openMasterSlave = openMasterSlave;
	}

	public boolean isOpenSharding() {
		return openSharding;
	}

	public void setOpenSharding(boolean openSharding) {
		this.openSharding = openSharding;
	}

	public String[] getMapperLocations() {
		return this.mapperLocations;
	}

	public void setMapperLocations(String[] mapperLocations) {
		this.mapperLocations = mapperLocations;
	}

	public String getTypeHandlersPackage() {
		return this.typeHandlersPackage;
	}

	public void setTypeHandlersPackage(String typeHandlersPackage) {
		this.typeHandlersPackage = typeHandlersPackage;
	}

	public String getTypeAliasesPackage() {
		return this.typeAliasesPackage;
	}

	public void setTypeAliasesPackage(String typeAliasesPackage) {
		this.typeAliasesPackage = typeAliasesPackage;
	}

	public ExecutorType getExecutorType() {
		return this.executorType;
	}

	public void setExecutorType(ExecutorType executorType) {
		this.executorType = executorType;
	}

	public Properties getConfigurationProperties() {
		return configurationProperties;
	}

	public void setConfigurationProperties(Properties configurationProperties) {
		this.configurationProperties = configurationProperties;
	}

	/**
	 * 获取 Mappers 资源文件
	 * 
	 * @return
	 */
	public Resource[] resolveMapperXmlLocations() {
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

		// 所有的资源目录
		List<Resource> resources = new ArrayList<Resource>();

		// 合并配置
		Set<String> mapperLocations = Sets.newHashSet("classpath*:com/swak/persistence/config/*.Mapper.xml");
		for (String mapperLocation : mapperLocations) {
			try {
				Resource[] mappers = resourceResolver.getResources(mapperLocation);
				resources.addAll(Arrays.asList(mappers));
			} catch (IOException e) {
				// ignore
			}
		}
		return resources.toArray(new Resource[resources.size()]);
	}
}
